<template>
  <div>
    <el-form ref="myform" :model="formItem" label-width="100px" size="mini" class="form-view">
      <el-row :gutter="40">
        <el-col :span="8">
          <el-form-item label="计划开始时间">
            {{ formItem.startTime }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="计划结束时间">
            {{ formItem.endTime }}
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="是否中断业务">
            {{ formItem.interruptBusiness ? "是" : "否" }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="是否重大变更">
            {{ formItem.significantChange ? "是" : "否" }}
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="可能存在的风险">
            <div class="richWraper" v-html="formItem.risk" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="风险处置措施">
            <div class="richWraper" v-html="formItem.riskManagement" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="实施方案">
            <div class="richWraper" v-html="formItem.implementPlan" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="回退方案">
            <div class="richWraper" v-html="formItem.fallbackPlan" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="40">
        <el-col :span="24">
          <el-form-item label="附件列表">
            <el-button v-for="tag in formItem.attachments" :key="tag.name" size="mini" type="text" @click.native="downloadFile(tag)">
              {{ tag.name }}
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import { detail, downloadFile } from '@neu/dcom-ui/api/changePlan'
import { formatDate } from '@neu/biz-core/utils/utils'
const formItem = {}
export default {
  name: 'ChangePlanView',
  components: {},
  props: {
    taskId: {
      required: false,
      type: String,
      default: 'NULL'
    },
    workOrderNum: {
      required: false,
      type: String,
      default: ''
    }
  },
  data() {
    return {
      formItem: {
        ...formItem
      }
    }
  },
  watch: {

  },
  created() { this.fetch() },
  methods: {
    fetch() {
      if (this.workOrderNum !== 'NULL') {
        detail(this.workOrderNum, this.taskId).then(response => {
          this.formItem = {
            ...response.data
          }
          if (this.formItem.id !== null) {
            this.formItem.startTime = formatDate(this.formItem.startTime)
            this.formItem.endTime = formatDate(this.formItem.endTime)
          } else {
            this.formItem.startTime = ''
            this.formItem.endTime = ''
          }
        })
      } else {
        this.formItem = {
          ...formItem
        }
      }
    },
    reset() {
      this.$refs.myform.resetFields()
      this.formItem = { ...formItem }
    },
    /** 下载附件 */
    downloadFile(tag) {
      downloadFile(tag.fsId).then(res => {
        const content = res
        const blob = new Blob([content])
        const fileName = tag.name
        if ('download' in document.createElement('a')) { // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else { // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
      })
    }
  }
}
</script>
